﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class StaffOfWardBLL
    {
        HospitalDataContext DB = new HospitalDataContext();

        public string layjob(string id)
        {
            return DB.Jobs.Where(s => s.StaffID == id && s.FinishDate == null).Select(k => k.JobID).Max().ToString();
        }
        public string Insert(StaffOfWardDTO sow)
        {
            try
            {
                
                StaffOfWard sw = new StaffOfWard();
                string job = DB.Jobs.Where(s => s.StaffID == sow.StaffID && s.FinishDate == null).Select(k => k.JobID).Max().ToString();
                sw.JobID = job;
                sw.WardID= sow.WardId;
                sw.StartDate = sow.StartDate;

                DB.StaffOfWards.InsertOnSubmit(sw);
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public IQueryable ListStaffInWard(string wardid)
        {
            return DB.Staffs.Join(
                DB.Jobs,
                a => a.StaffID,
                b => b.StaffID,
                (a, b) => new
                {
                    b.JobID,
                    a.StaffID,
                    Name = a.FirstName + " " + a.LastName,
                    a.Address,
                    a.Tel,
                    b.PositionID
                }
                ).Join(
                DB.Positions,
                c => c.PositionID,
                d => d.PositionID,
                (c, d) => new
                {
                    c.JobID,
                    c.StaffID,
                    c.Name,
                    c.Address,
                    c.Tel,
                    d.Position1,

                }
                ).Join(
                DB.StaffOfWards,
                e => e.JobID,
                f => f.JobID,
                (e, f) => new
                {
                    e.StaffID,
                    e.Name,
                    e.Address,
                    e.Tel,
                    Position=e.Position1,
                    f.WardID,
                    f.FinishDate
                }
                ).Where(s=>s.WardID==wardid && s.FinishDate==null).Distinct().AsQueryable();
        }
    }
}
